package UI;

import java.util.*;
import Controller.*;
import Model.*;

public class AddressUI
{
    private AddressCtr addressCtr;
    private static AddressUI instance = null;
    private MainUI mainUI;
    
    private AddressUI()
    {
        addressCtr = addressCtr.getInstance();
        addressMenuStart();
    }
    
    public static AddressUI getInstance()
    {
        if(instance == null)
        {
            instance = new AddressUI();
        }
        return instance;
    }
    
    // start menu
    public void addressMenuStart()
    {
        boolean exit = false;
        while(!exit)
        {
            int choice = printAddressMenu();
            switch (choice)
            {
                case 1: getPersonInfo();
                        break;
                case 2: addPerson();
                        break;
                case 3: deletePerson();
                        break;
                case 4: updatePerson();
                        break;
                case 5: listAllPersons();
                        break;
                case 6: goBack();
                        break;
            }
        }
    }
    
    public int printAddressMenu()
    {
        Scanner keyboard = new Scanner(System.in);
        //System.out.print('\f');
        System.out.println("***Address Menu***");
        System.out.println("1. Show info about Person");
        System.out.println("2. Add Person");
        System.out.println("3. Delete Person");
        System.out.println("4. Update Person");
        System.out.println("5. List all persons");
        System.out.println("6. Go back");
        System.out.println("Choice #: ");
        
        int choice = keyboard.nextInt();
        return choice;
    }
    
    public void getPersonInfo()
    {
        System.out.print('\f');
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type person Name: ");
        String name = keyboard.nextLine();
        ArrayList<Person> allPersons = addressCtr.getPerson(name);
        System.out.println(" ID |    Name    |   Phone    |    Address   | Postal Code |   City   |");
        //for(int i=0;i<=allPersons.size();i++)
        //{
        //    System.out.println(" "+ allPersons.get(i).getId() +" | "+ allPersons.get(i).getName() +" | "+ allPersons.get(i).getPhone() +" | "+ allPersons.get(i).getAddress() +" | "+ allPersons.get(i).getPostalCode() +" |");
        //}
        
        for(Person person : allPersons)
        {
            System.out.println(" " + person.getId() + " | " + person.getName() + " | " + person.getPhone() + " | " + person.getAddress() + " | " + person.getPostalCode() + " | "+ person.getCity() + " | ");
        }
    }
    
    public void addPerson()
    {
        Scanner keyboard = new Scanner(System.in); 
        System.out.print('\f');
        System.out.println("Person Name: ");
        String name = keyboard.nextLine();
        System.out.println("Person Phone: ");
        String phone = keyboard.nextLine();
        System.out.println("Person Address: ");
        String address = keyboard.nextLine();
        System.out.println("Person Postal Code: ");
        String postalCode = keyboard.nextLine();
        System.out.println("Person City: ");
        String city = keyboard.nextLine();
        addressCtr.createPerson(name, address, postalCode, city, phone);   
    }
    
    public void deletePerson()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print('\f');
        System.out.println("Person ID: ");
        int id = keyboard.nextInt();
        addressCtr.deletePerson(id);
    }
    
    public void updatePerson()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print('\f');
        System.out.println("Type persons ID number: ");
        int id = Integer.parseInt(keyboard.nextLine());
        
        System.out.println("Type new  name (in case you don't want to change it press enter)");
        String name = keyboard.nextLine();
        System.out.println("Type new  phone (in case you don't want to change it press enter)");
        String phone = keyboard.nextLine();
        System.out.println("Type new  address (in case you don't want to change it press enter)");
        String address = keyboard.nextLine();
        System.out.println("Type new  postalCode (in case you don't want to change it press enter)");
        String postalCode = keyboard.nextLine();
        System.out.println("Type new  city (in case you don't want to change it press enter)");
        String city = keyboard.nextLine();
        
        
        addressCtr.updatePerson(id, name, address, postalCode, city, phone);
    }
    
    public void listAllPersons()
    {
        System.out.print('\f');
        ArrayList<Person> allPersons = addressCtr.getAllPersons();
        System.out.println(" ID |    Name    |   Phone    |    Address   | Postal Code |  City  |");
        //for(int i=0;i<=allPersons.size();i++)
        //{
        //    System.out.println(" "+ allPersons.get(i).getId() +" | "+ allPersons.get(i).getName() +" | "+ allPersons.get(i).getPhone() +" | "+ allPersons.get(i).getAddress() +" | "+ allPersons.get(i).getPostalCode() +" |");
        //}
        
        for(Person person : allPersons)
        {
            System.out.println(" " + person.getId() + " | " + person.getName() + " | " + person.getPhone() + " | " + person.getAddress() + " | " + person.getPostalCode() + " | " + person.getCity() + " | ");
        }
    }
    
    // calls and returns to main menu
    public void goBack()
    {
        mainUI.getInstance().mainMenuStart();
    }
}
